#include "KEY.h"

void KEYInit(void){
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO,ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Pin= GPIO_Pin_8;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPD;

	GPIO_Init(GPIOB,&GPIO_InitStructure);
	
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource8);
	
	EXTI_InitTypeDef EXTI_InitStrcuture;
	EXTI_InitStrcuture.EXTI_Line= EXTI_Line8;
	EXTI_InitStrcuture.EXTI_Mode=EXTI_Mode_Interrupt;
	EXTI_InitStrcuture.EXTI_Trigger=EXTI_Trigger_Rising;
	EXTI_InitStrcuture.EXTI_LineCmd=ENABLE;
	EXTI_Init(&EXTI_InitStrcuture);
	
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	
	NVIC_InitTypeDef NVIC_InitStructure;
	NVIC_InitStructure.NVIC_IRQChannel=EXTI9_5_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
	NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
	
	NVIC_Init(&NVIC_InitStructure);
	
}

void EXTI9_5_IRQHandler(void) {

    // 检查是否是 PB8 触发的中断
    if (EXTI_GetITStatus(EXTI_Line8) == SET) {
        // 清除 EXTI8 中断标志
        EXTI_ClearITPendingBit(EXTI_Line8);
        
		// 按键持续按下8s,清空FLASH
		int time=0;
        while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_8) == Bit_SET){
			time++;
			if(time>6000000){
				erasePage(0x0800F000);
				printf3("Key Work!\r\n");
				break;
			}
		};

    }
}


